<?php

class Nhanvien_model extends CI_Model {

    function CheckLogin($username, $password) {
        $password = md5($password);
        $query = $this->db->get_where('NhanVien', array('Username' => $username, 'Password' => $password, 'TinhTrangTaiKhoan' => 1));
        if ($query->num_rows() > 0)
            return 1;
        return 0;
    }

    function GetChucVu($username) {
        $query = $this->db->get_where('NhanVien', array('Username' => $username));

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $chucvu = $row->ChucVu;
            }
            return $chucvu;
        }
        return NULL;
    }

    function LayMaNV($username) {
        $query = $this->db->get_where('NhanVien', array('Username' => $username));

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $manv = $row->MaNV;
            }
            return $manv;
        }
        return NULL;
    }
	
	function LayUsername($manv)
	{
			$ncc = $this->GetARecord($manv);
			$username = NULL;
			if($ncc)
			{
				foreach($ncc as $row)
					$username = $row->Username;
				return $username;
			}
			return $username;
	}
	
	function SetPass($manv)
		{
			$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
			$pass = array(); //remember to declare $pass as an array
			$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
			for ($i = 0; $i < 8; $i++)
			{
				$n = rand(0, $alphaLength);
				$pass[] = $alphabet[$n];
    		}
			$pass = implode($pass);
			$pass1 = md5($pass);
			$this->db->update('NhanVien', array('Password' => $pass1), array('MaNV' => $manv));
			return $pass;
		}
		function GetARecord($manv)
		{
			$query = $this->db->get_where('NhanVien',array('MaNV' => $manv));
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}

}

?>